package com.zhongxiangspace.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhongxiangspace.entity.County;
import com.zhongxiangspace.entity.House;
import com.zhongxiangspace.entity.VO.OfficeBHVO;
import com.zhongxiangspace.entity.VO.QueryAnalyzerOffice;
import com.zhongxiangspace.enums.AreaExtentEnum;
import com.zhongxiangspace.enums.PriceExtenEnum;
import com.zhongxiangspace.enums.RequestTypeEnum;
import com.zhongxiangspace.result.NormalResult;
import com.zhongxiangspace.result.PageResult;
import com.zhongxiangspace.result.Result;
import com.zhongxiangspace.service.CountyService;
import com.zhongxiangspace.service.HouseService;
import com.zhongxiangspace.service.OfficeBuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class BMapOfficeController {
    @Autowired
    private CountyService countyService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private OfficeBuildingService officeBuildingService;

    @PostMapping("/lists/ajxaCountyData")
    public Result getCounty(){
        //List<County> countyList=this.countyService.queryAllType(null);
        List<Map<String,Object>> countyByhouseNum=this.countyService.queryCountyByhouseNum();
        NormalResult normalResult=new NormalResult(countyByhouseNum);
        return  normalResult;
    }

    @PostMapping("lists/mapHouse")
    public Result  getPropertyID(QueryAnalyzerOffice queryAnalyzerNmapVO){
        if(queryAnalyzerNmapVO.getArea()!=null && !queryAnalyzerNmapVO.getArea().isEmpty()){
            queryAnalyzerNmapVO.setMaxArea(AreaExtentEnum.getEnumObject(queryAnalyzerNmapVO.getArea()).getMax());
            queryAnalyzerNmapVO.setMinArea(AreaExtentEnum.getEnumObject(queryAnalyzerNmapVO.getArea()).getMin());
        }
        if(queryAnalyzerNmapVO.getPrice()!=null && !queryAnalyzerNmapVO.getPrice().isEmpty()){
            queryAnalyzerNmapVO.setMinPrice(PriceExtenEnum.getEnumObject(queryAnalyzerNmapVO.getPrice()).getMin());
            queryAnalyzerNmapVO.setMaxPrice(PriceExtenEnum.getEnumObject(queryAnalyzerNmapVO.getPrice()).getMax());
        }
        PageHelper.startPage(queryAnalyzerNmapVO.getPage(),100 ,"office_buildings.hotCode DESC");
        Page<OfficeBHVO> officeBHVOPage = (Page<OfficeBHVO>) this.officeBuildingService.queryOfficeByLatLon(queryAnalyzerNmapVO);
        List<OfficeBHVO> officeBHVOList = officeBHVOPage.getResult();
        for (OfficeBHVO office : officeBHVOList) {
            queryAnalyzerNmapVO.setBuilding_id(office.getBuilding_id());
            queryAnalyzerNmapVO.setDistinct("coveredArea");
            List<House> houseList = this.houseService.queryBHList(queryAnalyzerNmapVO);
            office.setHouses(houseList);
        }
        PageResult pageResult = new PageResult(officeBHVOPage.getPageNum(), officeBHVOPage.getPageSize(), officeBHVOPage.getTotal(), officeBHVOPage.getPages(), officeBHVOPage.getResult(), true);
        NormalResult normalResult=new NormalResult(pageResult);
        System.out.println(queryAnalyzerNmapVO.toString());
        return  normalResult;
    }
}
